package controller;

import pojoCla.NewsChnl;
import pojoCla.SysUser;
import util.ChnlUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet(name = "DoChnl", value = "/doChnl")
public class DoChnl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String method = request.getParameter("method");
        SysUser user = (SysUser) request.getSession().getAttribute("user");
        if("1".equals(user.getSysCode())){
            response.sendRedirect("error.jsp");
            return;
        }
        Method dm = null;
        try {
            dm = this.getClass().getDeclaredMethod(method,HttpServletRequest.class,HttpServletResponse.class, ChnlUtil.class);
            dm.invoke(this,request,response,new ChnlUtil());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private void fistLoadChnlPage(HttpServletRequest request,HttpServletResponse response, ChnlUtil cu) throws ServletException{
        HttpSession session = request.getSession();
        try {
            List<NewsChnl> sumChnl = cu.getSumChnl(1, 5);
            session.setAttribute("chnlPageIndex",1);
            session.setAttribute("chnlPageList",sumChnl);
            session.setAttribute("chnlSumPage",cu.getSumPage(5));
            response.sendRedirect("/page/ColumnManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void updateChnlPage(HttpServletRequest request,HttpServletResponse response,ChnlUtil cu) throws ServletException{
        HttpSession session = request.getSession();
        String selChnlName = (String) session.getAttribute("selChnlName");
        String chnlPageIndex = request.getParameter("chnlPageIndex");
        try {
            Integer sumPage = selChnlName == null ? cu.getSumPage(5) : cu.selChnlSumPage(selChnlName,5);
            Integer pageIndex = chnlPageIndex == null ? (Integer) session.getAttribute("chnlPageIndex") : Integer.valueOf(chnlPageIndex);
            pageIndex = pageIndex > 0 ?     pageIndex <= sumPage ? pageIndex : sumPage    : 1;
            List<NewsChnl> sumChnl = selChnlName == null ? cu.getSumChnl(pageIndex, 5) : cu.selChnl(selChnlName,pageIndex,5);
            session.setAttribute("chnlPageIndex",pageIndex);
            session.setAttribute("chnlPageList",sumChnl);
            session.setAttribute("chnlSumPage",sumPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void doUpdatePage(HttpServletRequest request,HttpServletResponse response,ChnlUtil cu) throws ServletException{
        try {
            this.updateChnlPage(request,response,cu);
            response.sendRedirect("/page/ColumnManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void doAddChnl(HttpServletRequest request,HttpServletResponse response,ChnlUtil cu) throws ServletException{
        String addChnlName = request.getParameter("addChnlName");
        String addChnlNum = request.getParameter("addChnlNum");
        try {
            boolean b = cu.addChnl(addChnlNum, addChnlName);
            String msg = b ? "栏目新增成功" : "栏目新增失败";
            request.getSession().setAttribute("addChnlMSG",msg);
            request.getSession().setAttribute("chnlPageIndex",1);
            this.updateChnlPage(request,response,cu);
            response.sendRedirect("/page/ColumnManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void doDelChnl(HttpServletRequest request,HttpServletResponse response,ChnlUtil cu) throws ServletException{
        String delChnlName = request.getParameter("delChnlName");
        try {
            boolean b = cu.delChnl(delChnlName);
            String msg = b ? "栏目删除成功" : "栏目删除失败";
            request.getSession().setAttribute("delChnlMSG",msg);
            request.getSession().setAttribute("chnlPageIndex",1);
            this.updateChnlPage(request,response,cu);
            response.sendRedirect("/page/ColumnManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void doEditChnl(HttpServletRequest request,HttpServletResponse response,ChnlUtil cu) throws ServletException{
        String editChnlName = request.getParameter("editChnlName");
        String editChnlNum = request.getParameter("editChnlNum");
        String editChnlID = request.getParameter("editChnlID");
        try {
            boolean b = cu.editChnl(editChnlNum, editChnlName, editChnlID);
            String msg = b ? "栏目修改成功" : "栏目修改失败";
            request.getSession().setAttribute("editChnlMSG",msg);
            request.getSession().setAttribute("chnlPageIndex",1);
            this.updateChnlPage(request,response,cu);
            response.sendRedirect("/page/ColumnManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void doSearchChnl(HttpServletRequest request,HttpServletResponse response,ChnlUtil cu) throws ServletException{
        try {
            String selChnl = request.getParameter("selChnl");
            HttpSession session = request.getSession();
            session.setAttribute("chnlPageIndex",1);
            if(selChnl.isEmpty()){
                session.removeAttribute("selChnlName");
            }else{
                session.setAttribute("selChnlName",selChnl);
            }
            this.updateChnlPage(request,response,cu);
            response.sendRedirect("/page/ColumnManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void doFindChnl(HttpServletRequest request,HttpServletResponse response,ChnlUtil cu) throws ServletException{
        try {
            String findChnlName = request.getParameter("findChnlName");
            if(findChnlName.isEmpty()){
                findChnlName = request.getParameter("findChnlID");
            }
            String chnlName = cu.findChnlName(findChnlName);
            String msg =chnlName.isEmpty() ? "0":"1";
            PrintWriter writer = response.getWriter();
            writer.print(msg);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
